﻿using Microsoft.EntityFrameworkCore;
using NetTopologySuite.Operation.Relate;
using PHMEE.Entities.DaqInfo.TdDevices;
using PHMEE.EntityFrameworkCore;
using PHMEE.IRepositories.DaqInfo.TdDevices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace PHMEE.Repositories.DaqInfo.TdDevices
{
    public class TdSqlScriptRepository : EfCoreRepository<PHMEEDbContext, TdSqlScript, Guid>, ITdSqlScriptRepository
    {
        public TdSqlScriptRepository(IDbContextProvider<PHMEEDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        /// <summary>
        /// 根据数据库名称删除所有相关的 SQL 脚本
        /// </summary>
        /// <param name="dbName">数据库名称</param>
        /// <returns></returns>
        public async Task DeleteByDbNameAsync(string dbName)
        {
            var dbContext = await GetDbContextAsync();
            var scriptsToDelete = dbContext.Set<TdSqlScript>()
                .Where(script => script.DbName == dbName);

            dbContext.Set<TdSqlScript>().RemoveRange(scriptsToDelete);
            await dbContext.SaveChangesAsync();
        }
    }
}
